//
//  MLAManageAdvertVC.m
//  MagicLampApp
//
//  Created by Zheng on 2018/12/20.
//  Copyright © 2018 zheng.magic.lamp.app. All rights reserved.
//

#import "MLAManageAdvertVC.h"
#import "MLAManageAdvertCell.h"
#import "MLAManageAdvertApplyVC.h"
#import "MLAManageAdvertModel.h"

@interface MLAManageAdvertVC ()

@end

@implementation MLAManageAdvertVC {
    NSMutableArray<MLAManageAdvertModel *> *advertsArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self configTitle:@"广告位"];
    
    self.tableView.rowHeight = 142;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerClass:[MLAManageAdvertCell class] forCellReuseIdentifier:kManageAdvertCellID];
    
    self.haveRefreshHeader = YES;
    [self loadData];
}

#pragma mark - LoadData
- (void)loadData {
    [super loadData];
        
    [MLAHttpManage postHostType:MLAHttpHostType_Api api:kAdvertList params:nil success:^(id  _Nonnull response, NSString * _Nonnull msg) {
        self.page++;
        self.haveRefreshFooter = YES;
        self->advertsArr = [NSMutableArray array];
        self->advertsArr = [MLAManageAdvertModel mj_objectArrayWithKeyValuesArray:response[@"lists"]];
        if (self->advertsArr.count < 20) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }
        [self.tableView reloadData];

    } failure:^{
        
    }];
}

- (void)loadMoreData {
    NSDictionary *params = @{@"page":@(self.page)};
    [MLAHttpManage postHostType:MLAHttpHostType_Api api:kAdvertList params:params success:^(id  _Nonnull response, NSString * _Nonnull msg) {
        
        self.page++;
        
        if ([response[@"lists"] count] < 20) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        } else {
            [self.tableView.mj_footer endRefreshing];
        }
        
        [self->advertsArr addObjectsFromArray:[MLAManageAdvertModel mj_objectArrayWithKeyValuesArray:response[@"lists"]]];
        [self.tableView reloadData];
        
    } failure:^{
        
    }];
}


#pragma mark - tableview datasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return advertsArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MLAManageAdvertCell *cell = [tableView dequeueReusableCellWithIdentifier:kManageAdvertCellID forIndexPath:indexPath];
    [cell.applyBtn addTarget:self action:@selector(applyBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    MLAManageAdvertModel *model = advertsArr[indexPath.row];
    cell.model = model;
    return cell;
}

#pragma mark - Actions
- (void)applyBtnAction:(UIButton *)sender {
    NSIndexPath *indexPath = [self.tableView indexPathForCell:(UITableViewCell *)sender.superview.superview.superview];
    CX_Log(@"申请广告位 row=%ld",indexPath.row);
    MLAManageAdvertModel *model = advertsArr[indexPath.row];
    MLAManageAdvertApplyVC *applyVC = [[MLAManageAdvertApplyVC alloc] init];
    applyVC.modelId = model.modelId;
    applyVC.index = indexPath.row;
    [self.navigationController pushViewController:applyVC animated:YES];
    
    @weakify(self);
    applyVC.applySuccessBlock = ^(NSInteger index) {
        @strongify(self);
        MLAManageAdvertModel *model = self->advertsArr[index];
        model.status = @"2";
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
        [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
    };
}




@end
